import axios from "axios"
// 导入store
import store from '@/store/'
import jsonBig from 'json-bigint'


const http = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn'
})

http.defaults.transformResponse = [function (data) {
  try {
    return jsonBig.parse(data)
  } catch (err) {
    return {}
  }
}]

http.interceptors.request.use(config => {
  const { user } = store.state.loginStore
  if (user) {
    config.headers.Authorization = `Bearer ${user.token}`
  }

  return config
}, err => {
  return Promise.reject(err)
})

http.interceptors.response.use(response => {
  return response
}, err => {
  return Promise.reject(err)
})

export default http